package wzw.swing;

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.FlowLayout;
import java.awt.Graphics;
import java.awt.Insets;
import java.io.Serializable;
import java.net.URL;
import java.util.Vector;
import java.util.concurrent.TimeUnit;

import javax.swing.BorderFactory;
import javax.swing.JComponent;
import javax.swing.JEditorPane;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
import javax.swing.UIManager;
import javax.swing.event.HyperlinkEvent;
import javax.swing.event.HyperlinkListener;
import javax.swing.text.Document;

import org.apache.log4j.Logger;

import com.kingcore.director.DirectorConstants;
import com.kingcore.director.DirectorContext;
import com.kingcore.director.core.util.AdUtil;
import com.kingcore.director.core.util.DirectorComponentUtils;
import com.kingcore.director.main.MainFrame;
import com.sunking.swing.OpenSwingUtil;

/**
 * <p>Title: OpenSwing</p>
 * <p>Description: StatusBar 模拟Windows的状态条</p>
 * <p>Copyright: Copyright (c) 2004</p>
 * <p>Company: </p>
 * @author <a href="mailto:sunkingxie@hotmail.com">SunKing</a>
 * @version 1.0
 */
public class StatusBar
    extends JComponent
    implements Serializable {
	
	private final static Logger log = Logger.getLogger( StatusBar.class);
	
    private Vector vecCellWidth = new Vector();
    public StatusBar() {
        this.setPreferredSize(new Dimension(10, 20));
        this.setLayout(new FlowLayout(FlowLayout.LEFT, 0, 0));
        setBorder(BorderFactory.createLoweredBevelBorder());
    }

    /**
     * 添加一个状态栏
     * @param width int
     */
    public void addStatusCell(int width) {
        JLabel lb = new JLabel() {
            public void paint(Graphics g) {
                super.paint(g);
                int w = getWidth();
                int h = getHeight();
                g.setColor(Color.white);
                g.drawLine(w - 4, 0, w - 4, h - 2);
                g.setColor(new Color(128, 128, 128));
                g.drawLine(w - 1, 0, w - 1, h - 5);
            }

            public Insets getInsets() {
                return new Insets(0, 0, 0, 5);
            }
        };
        lb.setPreferredSize(new Dimension(width, getPreferredSize().height));
        add(lb);
        vecCellWidth.add("" + width);
    }
    public void addStatusCell2(int width, final MainFrame mainFrame) {
    	final JEditorPane lb = new JEditorPane() {
            public void paint(Graphics g) {
                super.paint(g);
                int w = getWidth();
                int h = getHeight();
                g.setColor(Color.white);
                g.drawLine(w - 4, 0, w - 4, h - 2);
                g.setColor(new Color(128, 128, 128));
                g.drawLine(w - 1, 0, w - 1, h - 5);
            }

            public Insets getInsets() {
                return new Insets(0, 0, 0, 5);
            }
        };
        lb.setEditable(false);
        lb.setBackground(new Color(214,217,223,0));
        lb.setPreferredSize(new Dimension(width, getPreferredSize().height));
        
//        ScheduledExecutorService scheduler = 
//            Executors.newScheduledThreadPool(5);
        Runnable task = new Runnable() {   
            @Override
            public void run() {   
				try {
					//lb.setText("<body><a href='www.baidu.com'>fas</a></body>");
//					mainFrame.getReminderMainPanel().refreshView();
					//call following before change page.
					Document doc = lb.getDocument();
				    doc.putProperty(Document.StreamDescriptionProperty, null ); //doc.getProperty(Document.StreamDescriptionProperty));
				    

				    lb.setText("");
				    lb.validate();
					lb.revalidate();
					lb.repaint();
					 doc.putProperty(Document.StreamDescriptionProperty, null); //doc.getProperty(Document.StreamDescriptionProperty));
					     
				    lb.setPage( new URL(AdUtil.getAdUrlByType(DirectorConstants.Ad_Block_StatusBar) ));
				    doc.putProperty(Document.StreamDescriptionProperty, null); //doc.getProperty(Document.StreamDescriptionProperty));
				   lb.validate();
					lb.revalidate();
					lb.repaint();
				} catch (Exception e) {
					log.error(e.getMessage() + "   url:" +AdUtil.getAdUrlByType(DirectorConstants.Ad_Block_StatusBar));
				}
            }   
        }; 
        DirectorContext.getScheduler().scheduleAtFixedRate(task, 0, DirectorConstants.Ad_Fetch_Interval, TimeUnit.MINUTES);  
 
		lb.addHyperlinkListener(new HyperlinkListener() {
		      public void hyperlinkUpdate(HyperlinkEvent e) {
		          if (e.getEventType() == HyperlinkEvent.EventType.ACTIVATED) {
		            try {
//		              String command = "explorer.exe "//如果是记事本打开,可用 notepad.exe
//		                  + e.getURL().toString();
//		              Runtime.getRuntime().exec(command);
		              DirectorComponentUtils.openWebUrl( e.getURL().toString() );

		            }
		            catch (Exception ex) {
		              ex.printStackTrace();
		            }
		          }
		        }
		      });
		
        add(lb);
        vecCellWidth.add("" + width);
    }

    /**
     * 取得状态条的栏数
     * @return int
     */
    public int getCellCount() {
        return getComponentCount();
    }

    /**
     * 设置状态信息
     * @param cellIndex int 栏号
     * @param status String 状态信息
     */
    public void setStatus(int cellIndex, String status) {
        if (getLabel(cellIndex) != null)
            getLabel(cellIndex).setText(status);
    }

    /**
     * 取得状态栏
     * @param cellIndex int 栏号
     * @return JLabel
     */
    private JLabel getLabel(int cellIndex) {
        if (cellIndex >= getCellCount())
            return null;
        JLabel lb = (JLabel) getComponent(cellIndex);
        return lb;
    }

    /**
     * 取得状态信息
     * @param cellIndex int 栏号
     * @return String
     */
    public String getStatusText(int cellIndex) {
        if (getLabel(cellIndex) != null)
            return getLabel(cellIndex).getText();
        return null;
    }

    public void paintChildren(Graphics g) {
        super.paintChildren(g);
        int w = getWidth();
        int h = getHeight();
        Color oldColor = g.getColor();
        // draw ///
        g.setColor(Color.white);
        g.drawLine(w, h - 12, w - 12, h);
        g.drawLine(w, h - 8, w - 8, h);
        g.drawLine(w, h - 4, w - 4, h);
        g.setColor(new Color(128, 128, 128));
        g.drawLine(w, h - 11, w - 11, h);
        g.drawLine(w, h - 10, w - 10, h);
        g.drawLine(w, h - 7, w - 7, h);
        g.drawLine(w, h - 6, w - 6, h);
        g.drawLine(w, h - 3, w - 3, h);
        g.drawLine(w, h - 2, w - 2, h);
        g.setColor(UIManager.getColor("Panel.background"));
        int cellW = 0;
        for (int i = 0; i < vecCellWidth.size(); i++) {
            cellW += Integer.parseInt(vecCellWidth.get(i).toString());
            g.drawLine(cellW - 2, 0, cellW, 0);
            g.drawLine(cellW - 2, 1, cellW, 1);
        }
        g.setColor(oldColor);
    }

    public static void main(String[] args) {
        StatusBar bar = new StatusBar();
        bar.addStatusCell(200);
        bar.addStatusCell(100);
        bar.addStatusCell(100);
        bar.setStatus(0, "Status 0 ");
        bar.setStatus(1, "Status 1 ");
        bar.setStatus(2, "Status 2 ");

        JFrame frame = OpenSwingUtil.createDemoFrame("StatusBar Demo");
        frame.getContentPane().add(bar, BorderLayout.SOUTH);
        frame.getContentPane().add(new JScrollPane(new JTextArea()),
                                   BorderLayout.CENTER);
        frame.setVisible(true);
    }
}
